<template>
    <!-- 登录 -->
    <div class="loginBar">
        <div class="login_top">
            <div class="operationIcon"></div>
            <div class="title">登陆</div>
            <div class="operationIcon operationIconHover" @click="loginStoreState.isTryingLoginOfFalse">
                <el-icon :size="40" color="#73767a"><Close /></el-icon>
            </div>
        </div>
        <div class="login_form_item">
            <form class="login_form_content">
                <div class="input_div">
                    <span class="login_span">
                        <img src="../icons/user/username.png" alt="" class="login_png">
                    </span>
                    <input type="text" name="username" class="login_input" aria-describedby="inputGroup-sizing-default" placeholder="用户名" v-model="username">
                </div>
                <div class="input_div">
                    <span class="login_span" id="login_password">
                        <img src="../icons/user/password.png" alt="" class="login_png">
                    </span>
                    <input type="password" name="password" class="login_input" aria-describedby="inputGroup-sizing-default" placeholder="密码" v-model="password">
                </div>
                <div class="input_div">
                    <input type="text" name="verify" class="login_input_verify" aria-describedby="inputGroup-sizing-default" placeholder="验证码" v-model="verifyNum">
                    <span class="login_span_verify" id="login_verify">
                        <img :src="verifyImgSrc" alt="" class="login_verify_png" @click="getCaptchaImg">
                    </span>
                </div>
                <div class="switch_div">
                    <text class="switch_jump" @click="userOperationStoreState.useRegister">没有账号？ </text>
                    <text class="switch_jump" @click="userOperationStoreState.useRetrieve">忘记密码？ </text>
                </div>
                <el-button type="primary" class="submit_btn" @click="loginUser"
                           :loading="offSticky"
                           :disabled="offSticky"
                           color="#5995fd"
                >
                    <text class="submit_text">登录</text>
                </el-button>
            </form>
        </div>
        <div></div>
        <div></div>
    </div>
</template>

<script setup>
import { ref } from 'vue'
import { Close, User, Lock } from '@element-plus/icons-vue'
import { loginState } from '@/stores/loginState.js'
import { userOperationState } from '@/stores/userOperationState.js'
import { ElMessage } from 'element-plus'
import { login } from '@/apis/login'
import {getCaptcha} from "@/apis/captcha.js";
import { setToken } from '@/utils/auth'

// 引入全局状态登陆状态和操作状态
const loginStoreState = loginState()
const userOperationStoreState = userOperationState()

// 登陆信息
const username = ref('admin')
const password = ref('admin123')
const uuid = ref()
const verifyNum = ref('')

const offSticky = ref(false)

// 验证码变量
const captchaSrc = ref('data:image/gif;base64,')
const verifyImgSrc = ref(captchaSrc.value)

const getCaptchaImg = async () => {
  captchaSrc.value = 'data:image/gif;base64,'
  verifyImgSrc.value = captchaSrc.value
  getCaptcha().then(res => {
    captchaSrc.value = captchaSrc.value + res.img
    verifyImgSrc.value = captchaSrc.value
    uuid.value = res.uuid
  })
}

getCaptchaImg()

const loginUser = async () =>{
    offSticky.value = true;
    let usernameBoolean = username.value === ''
    let passwordBoolean = password.value === ''
    let verifyNumbBoolean = verifyNum.value === ''
    if(usernameBoolean || passwordBoolean || verifyNumbBoolean){
        ElMessage({
            message: '您未或正确输入' + (usernameBoolean?' 账号':'') + (passwordBoolean?' 密码':'') + (verifyNumbBoolean?' 验证码':''),
            type: 'warning',
        })
        offSticky.value = false;
        return
    }

    //发送请求
    await login(username.value, password.value, verifyNum.value, uuid.value)
    .then((result) => {
        if(result.code === 200){
          loginStoreState.turnLoginStateToTrue()
          loginStoreState.isTryingLoginOfFalse()
          setToken(result.token)
          ElMessage.success("登陆成功")
        }else{
            loginStoreState.turnLoginStateToFalse()
        }
    })
    offSticky.value = false;
    location.reload();
}


</script>

<style scoped>
/* 登录 */
.loginBar{
    height: 55vh;
    width: 35%;
    background-color: #ffffff;
    border-radius: 4px;
    box-shadow: 0px 2px 4px 0 rgba(0, 0, 0, 0.2);
    border: 1px solid #efefef;
}

.login_top{
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 2vh 2vh 0 2vh;
}
.title{
    font-size: 38px;
    color: #444444;
    letter-spacing: 8px;
    font-family: "Microsoft YaHei", sans-serif;
}
.operationIcon{
    height: 50px;
    width: 50px;
    border-radius: 4px;
    transition: all 0.4s ease-in-out;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}
.operationIconHover:hover{
    background-color: #e9e9eb;
}

.login_form_item{
    padding-top: 20px;
    height: 70%;
    width: 100%;
}
.login_form_content{
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
}
.input_div{
    height: 20%;
    width: 60%;
    background-color: #f0f0f0;
    display: flex;
    border-radius: 4px;
    margin-bottom: 2%;
}
.login_span{
    height: 100%;
    width: 15%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}
.login_png{
    height: 40%;
    width: 40%;
}
.login_input{
    height: 100%;
    width: 80%;
    border: none;
    background-color: rgb(0,0,0,0);
    font-size: 1.1rem;
}
.login_input_verify{
    height: 100%;
    width: 60%;
    border: none;
    background-color: rgb(0,0,0,0);
    font-size: 1.1rem;
    padding-left: 8%;
}
.login_span_verify{
    height: 100%;
    width: 40%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    cursor: pointer;
}
.login_verify_png{
    height: 100%;
    width: 100%;
    border-radius: 4px;
    background-color: #dedfe0;
    border:1px solid #b1b3b8;
}
.switch_div{
    height: auto;
    width: 60%;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.switch_jump{
    cursor: pointer;
    transition: all 0.2s ease-in-out;
}
.switch_jump:hover{
    color: #409EFF;
}

.submit_btn{
    background-color: #5995fd;
    width: 60%;
    height: 20%;
    margin-top: 1.5%;
    border-radius: 4px;
    border: none;
    transition: background-color 0.2s ease-in-out;
}
.submit_btn:hover{
    background-color: rgb(82, 106, 157);
}
.submit_btn:active{
    background-color: #3b5f9d;
}

.submit_text{
    font-size: 17px;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    color: #fff;
    font-weight: 600;
}
</style>
